//
//  HHDetailUserCell.m
//  HHChatKit
//
//  Created by Henry on 2021/9/23.
//

#import "HHDetailUserCell.h"
#import <HHTool/HHTool.h>
#import "HHChatConfiguration.h"
#import "HHUserCollectionViewCell.h"
#import "HHResources.h"
#import "HHDetailUserModel.h"

NSString * const HHFormRowDescriptorTypeDetailUser = @"HHFormRowDescriptorTypeDetailUser";

static NSString * const userIdentifier = @"HHUserCollectionViewCell";

@interface HHDetailUserCell () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UIButton *moreButton;

@property (nonatomic, strong) HHDetailUserModel *model;

@end

@implementation HHDetailUserCell

+ (void)load {
    [XLFormViewController.cellClassesForRowDescriptorTypes setObject:[HHDetailUserCell class] forKey:HHFormRowDescriptorTypeDetailUser];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (self.model.more) {
        HHChatDetailTheme *theme = self.model.theme;

        self.collectionView.frame = CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height - theme.moreHeight);
        self.moreButton.frame = CGRectMake(0, CGRectGetMaxY(self.collectionView.frame), self.contentView.bounds.size.width, theme.moreHeight);

    } else {
        self.collectionView.frame = self.contentView.bounds;
    }
}

#pragma mark - XLFormDescriptorCell

- (void)configure {
    [super configure];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [self.contentView addSubview:self.collectionView];
    [self.contentView addSubview:self.moreButton];
}

- (void)update {
    self.model = (HHDetailUserModel *)self.rowDescriptor.value;
        
    [self.model setupHeight:self.model.width];
    
    self.moreButton.hidden = !self.model.more;
    
    [self.collectionView reloadData];

}

+ (CGFloat)formDescriptorCellHeightForRowDescriptor:(XLFormRowDescriptor *)rowDescriptor {
    HHDetailUserModel *model = (HHDetailUserModel *)rowDescriptor.value;
    return model.cellHeight;
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.model.displayDataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HHUserCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:userIdentifier forIndexPath:indexPath];
    
    cell.model = self.model.displayDataArray[indexPath.row];
    [cell setupTheme:self.model.theme];

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (self.model.didSelectUser) {
        self.model.didSelectUser(self.model.displayDataArray[indexPath.row]);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    HHChatDetailTheme *theme = self.model.theme;
    return CGSizeMake(theme.imageHeight, theme.imageHeight + theme.nameHeight);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    HHChatDetailTheme *theme = self.model.theme;
    return UIEdgeInsetsMake(theme.spacing, theme.spacing, theme.spacing, theme.spacing);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return self.model.theme.spacing;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return self.model.theme.spacing;
}

#pragma mark - onClickMoreButton

- (void)onClickMoreButton {
    if (self.model.didSelectMore) {
        self.model.didSelectMore(self.model.dataArray);
    }
}

#pragma mark - Getters

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = self.backgroundColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[HHUserCollectionViewCell class] forCellWithReuseIdentifier:userIdentifier];
    }
    return _collectionView;
}

- (UIButton *)moreButton {
    if (!_moreButton) {
        _moreButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_moreButton setTitle:GetHCLocalizableString(@"GroupProfileMoreMember") forState:UIControlStateNormal];
        [_moreButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _moreButton.hidden = YES;
        _moreButton.backgroundColor = self.backgroundColor;
        [_moreButton setImage:[HHResources getImageFromMessage:@"icon_arrow_right"] forState:UIControlStateNormal];
        _moreButton.tintColor = [UIColor lightGrayColor];
        [_moreButton addTarget:self action:@selector(onClickMoreButton) forControlEvents:UIControlEventTouchUpInside];
        _moreButton.hh_titleWithImageAlignment = HHTitleWithImageAlignmentLeft;
    }
    return _moreButton;
}

@end
